package net.bbtang.website.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bbtang.website.bean.UserGroupBean;
import net.bbtang.website.pojo.UserGroupApplyDTO;
import net.bbtang.website.pojo.UserGroupRefDTO;
import net.bbtang.website.service.GroupApplyInfoService;
import net.bbtang.website.service.GroupInfoService;
import net.bbtang.website.service.GroupRefInfoService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/groupInfo")
public class GroupInfoApiController {
	@Autowired
	GroupInfoService groupInfoService;
	@Autowired
	GroupRefInfoService groupRefInfoService;
	@Autowired
	GroupApplyInfoService groupApplyInfoService;
	
	private static final Logger logger = Logger.getLogger(GroupInfoApiController.class); 
	
	/**根据学校id获取当前学校小组信息或推荐小组信息
	 @api {post} /groupInfo/getGroupInfoByUniversity 根据学校id获取当前学校小组信息或推荐小组信息
	 
	 @apiParam {String} universityId 学校id 必填
	 @apiParam {int} groupType 小组类型（0：当前学校全部小组 1：当前学校首页推荐小组） 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"universityId":1,
		"groupType":1
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		  "number": "2000",
		  "flag": true,
		  "data": [
		    {
		      "id": 2,                          小组id
		      "groupName": "吃货",               小组名称
		      "groupAdmin": 111,                小组管理员id
		      "groupInUniversity": 1,           所属学校id
		      "remarks": "我吃货，我骄傲",           介绍说明
		      "createTime": 1492165071000,      创建时间
		      "updateTime": 1492165073000,      更新时间
		      "userGroupMeal": [
		        {
		          "mealId": 2,                  套餐id
		          "groupId": 2,                 所属小组id
		          "mealName": "套餐A",           套餐名称
		          "mealPrice": 9,               套餐金额
		          "mealLogo": "www.baidul.com", 套餐logo
		          "mealRemark": "草莓+苹果",       套餐介绍说明
		          "createTime": 1492165149000,  创建时间
		          "updateTime": 1492165153000         更新时间
		        },
		        {
		          "mealId": 3,
		          "groupId": 2,
		          "mealName": "套餐B",
		          "mealPrice": 15,
		          "mealLogo": "www.baidu.com",
		          "mealRemark": "奥利奥+西瓜+草莓",
		          "createTime": 1492165178000,
		          "updateTime": 1492165181000
		        }
		      ]
		    },
		    {
		      "id": 3,
		      "groupName": "摄影",
		      "groupAdmin": 111,
		      "groupInUniversity": 1,
		      "remarks": "我摄影，我骄傲",
		      "createTime": 1492165092000,
		      "updateTime": 1492165094000,
		      "userGroupMeal": [
		        {
		          "mealId": 4,
		          "groupId": 3,
		          "mealName": "套餐A",
		          "mealPrice": 500,
		          "mealLogo": "www.baidu.com",
		          "mealRemark": "摄影",
		          "createTime": 1492165202000,
		          "updateTime": 1492165205000
		        },
		        {
		          "mealId": 5,
		          "groupId": 3,
		          "mealName": "套餐B",
		          "mealPrice": 1000,
		          "mealLogo": "www.baidu.com",
		          "mealRemark": "毕业照跟拍",
		          "createTime": 1492165223000,
		          "updateTime": 1492165225000
		        }
		      ]
		    },
		    {
		      "id": 1,
		      "groupName": "打水",
		      "groupAdmin": 111,
		      "groupInUniversity": 1,
		      "remarks": "我打水，我骄傲",
		      "createTime": 1492165049000,
		      "updateTime": 1492165052000,
		      "userGroupMeal": [
		        {
		          "mealId": 1,
		          "groupId": 1,
		          "mealName": "1壶水",
		          "mealPrice": 1,
		          "mealLogo": "www.baidu.com",
		          "mealRemark": "打一壶水",
		          "createTime": 1492165120000,
		          "updateTime": 1492165123000
		        }
		      ]
		    }
		  ]
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取小组信息异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getGroupInfoByUniversity", method = RequestMethod.POST)
	@ResponseBody
	public Object getGroupInfoByUniversity(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			String universityId = jb.getString("universityId");
			int groupType = jb.getInt("groupType");//小组类型（0：当前学校全部小组，1：当前学校被推荐首页小组）
			List<UserGroupBean> userGroup = groupInfoService.getGroupInfoByUniversity(universityId,groupType);
			sc.setData(userGroup);
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取小组信息异常");
		}
		
		return sc;
	
	}
	
	
	/**根据小组id获取小组信息
	 @api {post} /groupInfo/getGroupInfoById 根据小组id获取小组信息
	 
	 @apiParam {String} gropupId 小组id 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"groupId":"1"
		}

	@apiSuccessExample {json} 请求成功结果Demo:
	

    {
        "number": "2000",
        "flag": false,
        "data":
        [
            {
                "id": 1,
                "groupName": "打水",
                "groupAdmin": 111,
                "groupInUniversity": 1,
                "remarks": "我打水，我骄傲",
                "createTime": 1492165049000,
                "updateTime": 1492165052000,
                "userGroupMeal":
                [
                    {
                        "mealId": 1,
                        "groupId": 1,
                        "mealName": "1壶水",
                        "mealPrice": 1,
                        "mealLogo": "www.baidu.com",
                        "mealRemark": "打一壶水",
                        "createTime": 1492165120000,
                        "updateTime": 1492165123000
                    }
                ]
            }
        ]
    }



	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取小组详情异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getGroupInfoById", method = RequestMethod.POST)
	@ResponseBody
	public Object getGroupInfoById(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			String groupId = jb.getString("groupId");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("groupId", groupId);
			List<UserGroupBean> userGroup = groupInfoService.getGroupInfoByid(map);
			sc.setData(userGroup);
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取小组信息异常");
		}
		
		return sc;
	
	}
	
	/**申请加入小组
	 @api {post} /groupInfo/applyJoinGroup 申请加入小组
	 
	 @apiParam {int} userId 用户id 必填
	 @apiParam {int} groupId 小组id 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":1,
		"groupId":1
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
	  "number": "2000",
	  "flag": true,
	  "detailMsg": "申请成功"
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "申请加入小组异常"
	}

    {
        "number": "5003",
        "flag": false,
        "detailMsg": "已加入或重复申请"
    }



	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/applyJoinGroup", method = RequestMethod.POST)
	@ResponseBody
	public Object applyJoinGroup(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer userId = jb.getInt("userId");
			Integer groupId = jb.getInt("groupId");
			UserGroupRefDTO ref = groupRefInfoService.getGroupRefById(userId, groupId);
			if(ref != null){
				sc.codes(ConstantsStatus.SC5003);
				sc.setDetailMsg("已加入或重复申请");
			}else{
				groupRefInfoService.applyJoinGroup(userId, groupId);
				sc.setFlag(true);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("申请加入小组异常");
		}
		
		return sc;
	
	}
	
	/**确认加入小组
	 @api {post} /groupInfo/confirmJoinGroup 确认加入小组
	 
	 @apiParam {int} userId 用户id 必填
	 @apiParam {int} groupId 小组id 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":1,
		"groupId":1
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		  "number": "2000",
		  "flag": true,
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "确认加入小组异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/confirmJoinGroup", method = RequestMethod.POST)
	@ResponseBody
	public Object confirmJoinGroup(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer userId = jb.getInt("userId");
			Integer groupId = jb.getInt("groupId");
			groupRefInfoService.confirmJoinGroup(userId, groupId);
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("确认加入小组异常");
		}
		
		return sc;
	
	}
	
	
	/**获取小组成员列表
	 @api {post} /groupInfo/getGroupMember 获取小组成员列表
	 
	 @apiParam {int} groupId 小组id 必填
	 @apiParam {int} applyState 申请状态（0：申请中 1：加入） 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"groupId":1,
		"applyState":1
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		 "number": "2000",
		  "flag": false,
		  "data": [
		    {
		      "userId": 1,用户id
		      "groupId": 1,小组id
		      "adminType": 0,管理员状态（0：普通成员 1：管理员）
		      "applyState": 1,申请状态（0：申请中 1：已加入）
		      "createTime": 1492175948000,创建时间
		      "updateTime": 1492175948000,更新时间
		      "user": [
		        {
		          "id": 1,用户id
		          "userName": "张小盒",用户名
		          "sex": 0,性别 0：女 1：男
		          "headImage": "www.baidu.com",头像
		          "studentCardMain": "www.baidu.com",学生证（正面）
		          "studentCardBack": "www.baidu.com",学生证（背面）
		          "telphone": "15022222222",手机号
		          "dormitoryNo": "1-2-301",宿舍号
		          "wechatNo": "12345",微信号
		          "qqNo": "54321",Qq号
		          "personalSignature": "好孩子",个人简介
		          "universityId": 1,学校id
		          "changeUniversityNum": 0,学校修改次数
		          "isRealName": 1,是否实名认证（0：否 1：是）
		          "isGroup": 1,是否是小组成员（0：否 1：是）
		          "qrcode": "www",个人二维码
		          "openId": "qweqweqweqwe",移动端授权登录唯一标识
		          "createTime": 1492176561000,创建时间
		          "updateTime": 1492176564000  更新时间
		        }
		      ]
		    }
		  ]
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取小组成员异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getGroupMember", method = RequestMethod.POST)
	@ResponseBody
	public Object getGroupMember(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer groupId = jb.getInt("groupId");
			Integer applyState = jb.getInt("applyState");//0:申请  1：加入
			sc.setFlag(true);
			sc.setData(groupRefInfoService.getGroupMember(groupId,applyState));
			
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取小组成员异常");
		}
		
		return sc;
	
	}
	
	
	/**申请创建小组
	 @api {post} /groupInfo/applyCreateGroup 申请创建小组
	 
	 @apiParam {int} userId 用户id 必填
	 @apiParam {int} groupMemberNum 申请人组建小组成员人数 必填
	 @apiParam {String} groupDescribe 小组描述 必填
	 @apiParam {String} userAdvantage 个人优势 必填
	 @apiParam {String} groupAdvantage 小组优势 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":1,
		"groupMemberNum":10,
		"groupDescribe":"小组描述",
		"userAdvantage":"个人优势",
		"groupAdvantage":"小组优势"
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		  "number": "2000",
		  "flag": true,
		  "detailMsg": "小组申请成功"
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "申请创建小组异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/applyCreateGroup", method = RequestMethod.POST)
	@ResponseBody
	public Object applyCreateGroup(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			UserGroupApplyDTO userGroupApply = (UserGroupApplyDTO)JSONObject.toBean(jb, UserGroupApplyDTO.class);
			groupApplyInfoService.addGroupApplyInfo(userGroupApply);
			sc.setFlag(true);
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("申请创建小组异常");
		}
		
		return sc;
	
	}
	
	/**
	 * 确认创建小组
	 * @param body
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/confirmCreateGroup", method = RequestMethod.POST)
	@ResponseBody
	public Object confirmCreateGroup(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			//后台联系确认创建小组
			
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("申请创建小组异常");
		}
		
		return sc;
	
	}
	
	
	
	
	
	
}
